<template>
  <tm-app ref="app">
    <!-- #ifndef H5 -->
    <breadCrumb :dataList="data.titleArray"></breadCrumb>
    <!-- #endif -->
    <personList :setList="data.listInfo" @btnClick="goMenu"></personList>
  </tm-app>
</template>

<script lang="ts" setup>
import { onLoad, onShow } from '@dcloudio/uni-app'
import { reactive } from 'vue'
import orgCtrl from '../../../ts/controller'
import { ICompany } from '../../../ts/core/target/team/company'
import { IPerson } from '../../../ts/core/target/person'
import personList from '../../relation/components/personList.vue'
import breadCrumb from '../../../components/breadCrumb/index.vue'

const data = reactive<any>({
  listInfo: [],
  titleArray: [],
  queryItem: '',
  nextLength: 0
})
onShow(() => {
  /* eslint-disable */
  const pages = getCurrentPages()
  let titleArray: any = [] //标题数据
  pages.forEach((_item, index) => {
    if (index > 0) {
      const currentPage: any = pages[pages.length - index]
      const options = currentPage.options
      let data: any = JSON.parse(options.item)
      let jsons: any = {}
      jsons = {
        name: data.label
      }
      titleArray.push(jsons)
    }
  })
  data.titleArray = titleArray.reverse()
})

onLoad(async query => {
  if (query && query.item) {
    uni.showLoading({ title: '加载中...' })
    let item = JSON.parse(query.item)
    data.queryItem = item
    loadGroupsOrCohorts(item)
  }
})
async function getMenuList(objs: any) {
  let arr: {
    label: any
    itemType: any
    icon: any
    remark: any
    isShow: boolean
    key: any
    code: any
    createTime: any
    updateTime: any
  }[] = []
  objs.children?.forEach(element => {
    let obj = {
      label: element.name,
      itemType: element.typeName,
      icon: element?.share?.avatar?.thumbnail,
      remark: element.remark,
      isShow: true,
      key: element.key,
      code: element.code,
      createTime: element.createTime,
      updateTime: element.updateTime
    }
    arr.push(obj)
  })
  uni.hideLoading()
  if (data.queryItem.itemType === 'next') {
    data.listInfo = arr
  } else {
    data.listInfo = arr.filter(item => {
      return item.itemType.includes(data.queryItem.label)
    })
  }
}

async function goMenu(
  item: {
    key: any
    label: any
    type: any
    mainKey: any
    itemType: any
    code: any
    createTime: any
    updateTime: any
    remark: any
  },
  type: any
) {
  const params = {
    key: item.key,
    label: item.label,
    itemType: data.nextLength > 0 ? 'next' : 'member',
    companyKey: data.queryItem.key,
    curType: data.queryItem.label
  }

  if (type === 'menuDetail') {
    const params = {
      name: item.label,
      typeName: item.itemType,
      code: item.code,
      createTime: item.createTime,
      updateTime: item.updateTime,
      remark: item.remark
    }
    uni.navigateTo({
      url: '/pages/relation-pages/menuDetail?item=' + JSON.stringify(params)
    })
  } else {
    uni.navigateTo({
      url: '/pages/relation-pages/group/groupList?item=' + JSON.stringify(params)
    })
  }
}

/** 加载群组 / 组织群 */
async function loadGroupsOrCohorts(info: any) {
  let curObjInfo: ICompany | IPerson | undefined = undefined
  if (info.itemType === '个人') {
    await orgCtrl.user.loadTeams()
    curObjInfo = orgCtrl.user
  } else {
    curObjInfo = orgCtrl.user.companys.find(i => i.directory.key === info.key)
    await curObjInfo?.loadDepartments()
    await curObjInfo?.loadGroups()
  }
  if (curObjInfo) {
    let result = {
      key: curObjInfo.directory.key,
      item: curObjInfo.directory,
      label: curObjInfo.name,
      itemType: curObjInfo.directory.typeName,
      tag: [curObjInfo.typeName],
      icon: '',
      remark: curObjInfo.remark,
      children:
        info.label === '群组' ? curObjInfo?.cohorts : curObjInfo?.groups,
      beforeLoad: async () => {
        await curObjInfo?.loadContent()
      }
    }
    getMenuList(result)
  }
}
</script>
<style lang="scss">
@import '../../../common/style/global.scss';
</style>
